package com.guguskins.back.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guguskins.back.common.exception.GlobalMsgException;
import com.guguskins.back.entity.GuBot;
import com.guguskins.back.mapper.GuBotMapper;
import com.guguskins.back.mapper.GuStockMapper;
import com.guguskins.back.service.GuBotService;
import com.guguskins.back.utils.steam.Bot.SteamBotResponse;
import com.guguskins.back.utils.steam.SteamBotUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-08-02
 */
@Service
@Slf4j
public class GuBotServiceImpl extends ServiceImpl<GuBotMapper, GuBot> implements GuBotService {

    @Autowired
    private GuStockMapper stockMapper;

    @Override
    public boolean backUpdate() {
        AtomicBoolean result = new AtomicBoolean(false);
        SteamBotResponse steamBotResponse = SteamBotUtil.list();
        if (!steamBotResponse.getCode().equals(0)) {
            log.error("海盗海添加账号失败：{}", steamBotResponse.getMessage());
            throw new GlobalMsgException("更新账号失败");
        }
        JSONObject body = steamBotResponse.getBody();
        JSONArray bots = body.getJSONArray("bots");
        for (int i = 0; i < bots.size(); i++) {
            JSONObject bot = bots.getJSONObject(i);
            GuBot guBot = new GuBot();
            guBot.setBotSteamId(bot.getString("steamid"));
            guBot.setBotEnable(bot.getBoolean("enable"));
            guBot.setBotOnline(bot.getBoolean("online"));
            if (this.count(new LambdaQueryWrapper<GuBot>() {
                {
                    eq(GuBot::getBotSteamId, guBot.getBotSteamId());
                }
            }) > 0) {
                result.getAndSet(this.update(guBot, new LambdaUpdateWrapper<GuBot>() {
                    {
                        eq(GuBot::getBotSteamId, guBot.getBotSteamId());
                    }
                }));
            } else {
                result.getAndSet(guBot.insert());
            }
        }
        return result.get();
    }

    @Override
    public List<GuBot> getList() {
        List<GuBot> result = new ArrayList<>();
        SteamBotResponse steamBotResponse = SteamBotUtil.list();
        if (!steamBotResponse.getCode().equals(0)) {
            log.error("海盗海添加账号失败：{}", steamBotResponse.getMessage());
            throw new GlobalMsgException("更新账号失败");
        }
        JSONObject body = steamBotResponse.getBody();
        JSONArray bots = body.getJSONArray("bots");
        for (int i = 0; i < bots.size(); i++) {
            JSONObject bot = bots.getJSONObject(i);
            GuBot guBot = new GuBot();
            guBot.setBotSteamId(bot.getString("steamid"));
            guBot.setBotEnable(bot.getBoolean("enable"));
            guBot.setBotOnline(bot.getBoolean("online"));
            result.add(guBot);
        }
        return result;
    }
}
